package cn.quevo.beetl.spring.boot.autoconfigure;

import org.beetl.core.om.ObjectUtil;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.PostgresStyle;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.Environment;

import cn.quevo.core.lang.StringUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ConfigurationProperties(prefix = "spring.beetlsql")
public class BeetlSqlProperties {
    
    // 扫描的Package	
	private String basePackage = "cn.quevo";				
	// Dao后缀名
	private String daoSuffix = "Batis";					
	// SQL文件路径
	private String sqlPath = "/sql";						
	// 开启Debug模式
	private boolean debug = true;						
	// 数据库方言
	private String dbStyle;								
	// 是否开发模式
	private boolean dev = true;							
	
	public BeetlSqlProperties(Environment env){
        basePackage = env.getProperty("spring.beetlsql.base-package", "com.");
        daoSuffix = env.getProperty("spring.beetlsql.dao-suffix", "Dao");
        sqlPath = env.getProperty("spring.beetlsql.sql-path", "/sql");
        dbStyle = env.getProperty("beetlsql.dbStyle", "org.beetl.sql.core.db.PostgresStyle");
        dev = env.getProperty("spring.beetlsql.dev", Boolean.class, true);
        debug = env.getProperty("spring.beetlsql.debug", Boolean.class, true);
	}
	
	/**
	 * 利用Class.forName实例化对象
	 * @return
	 */
	public DBStyle getDBStyleInstance() {
	    if(StringUtils.isBlank(this.dbStyle)) {
	        new PostgresStyle();
	    }
	    return (DBStyle) ObjectUtil.tryInstance(this.dbStyle);
	}
}
